package zdream.pmfield.stage.effect;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;

import zdream.pmfield.stage.Stage;
import zdream.pmfield.stage.director.AttackContext;
import zdream.pmfield.stage.director.HookStore;
import zdream.pmfield.stage.director.HookType;
import zdream.pmfield.stage.message.StageMessage;
import zdream.pmfield.stage.object.FoeActor;
import zdream.pmfield.stage.object.Seat;

/**
 * @author Zdream
 * @date 2023-11-17
 * @since 0.0.1
 */
public abstract class AbstractEffect {
	public final String category, name;

	public AbstractEffect(String category) {
		this(category, category);
	}

	public AbstractEffect(String category, String name) {
		this.category = category;
		this.name = name;
	}

	// 因为是由 FoeActor 触发的, 所以这里只能是 HookHandlerBuilder<FoeStageBuff>
	public abstract void init(Function<HookType, HookStore.HookHandlerBuilder> onHook);

	/* **********
	 * Targets  *
	 ********** */

	public enum EffectTarget {
		self,
		enemy,
	}

	protected List<FoeActor> affectedFoes(AttackContext context) {
		List<FoeActor> results = new ArrayList<>();

		final FoeActor targetFoe = context.action.targetFoe;
		if (targetFoe != null) {
			results.add(targetFoe);
			return results;
		}

		Seat seat = context.action.targetSeat;
		results.addAll(seat.findFoes());
		return results;
	}

	protected List<FoeActor> affectedFoesWithTarget(AttackContext context, EffectTarget target) {
		switch (target) {
		case self:
			return Collections.singletonList(context.attacker);
		case enemy:
			return affectedFoes(context);
		default:
			throw new IllegalArgumentException("尚未支持 target 为 " + target + " 的操作");
		}
	}

	/* **********
	 *  Others  *
	 ********** */

	protected void pushMessage(Stage stage, StageMessage message) {
		stage.pushMessage(message);
	}
}
